﻿namespace LevelEditors
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.Serialization;
    using UnityEngine.UI;

    public class UIGenericMenu : Singleton<UIGenericMenu>
    {
        [CompilerGenerated]
        private static Action<UIElement, int> <>f__am$cache5;
        private static List<UIElement> caches = new List<UIElement>();
        private static List<Element> elements = new List<Element>();
        [SerializeField]
        private Button m_closeButton;
        [FormerlySerializedAs("m_contentPrefab"), SerializeField]
        private UIElement m_elementPrefab;
        [SerializeField]
        private RectTransform m_group;

        [DebuggerHidden]
        private static IEnumerator _ClampToWindow(RectTransform target)
        {
            return new <_ClampToWindow>c__Iterator2 { target = target, <$>target = target };
        }

        public static void Add(string name, bool isOn, Action onClick)
        {
            Element item = new Element {
                name = name,
                isOn = isOn,
                onClick = onClick
            };
            elements.Add(item);
        }

        private static void ClampToWindow(RectTransform target)
        {
            Singleton<UIGenericMenu>.Instance.StartCoroutine(_ClampToWindow(target));
        }

        public static void Clear()
        {
            elements.Clear();
        }

        public static void Show()
        {
            Singleton<UIGenericMenu>.Instance.gameObject.SetActive(true);
            group.position = Input.mousePosition;
            ClampToWindow(group);
            if (<>f__am$cache5 == null)
            {
                <>f__am$cache5 = delegate (UIElement c, int i) {
                    <Show>c__AnonStorey37 storey = new <Show>c__AnonStorey37 {
                        element = elements[i]
                    };
                    c.label = storey.element.name;
                    if (storey.element.isOn)
                    {
                        c.label = "√ " + c.label;
                    }
                    c.isButton = true;
                    c.onLeftClick = new Action(storey.<>m__16);
                };
            }
            UIUtility.RefreshElements<UIElement>(caches, elements.Count, elementPrefab, group, <>f__am$cache5);
        }

        private void Start()
        {
            closeButton.gameObject.AddComponent<UIPointerDownEventListener>().onPointerDown = evt => base.gameObject.SetActive(false);
        }

        private static Button closeButton
        {
            get
            {
                return Singleton<UIGenericMenu>.Instance.m_closeButton;
            }
        }

        private static UIElement elementPrefab
        {
            get
            {
                return Singleton<UIGenericMenu>.Instance.m_elementPrefab;
            }
        }

        private static RectTransform group
        {
            get
            {
                return Singleton<UIGenericMenu>.Instance.m_group;
            }
        }

        [CompilerGenerated]
        private sealed class <_ClampToWindow>c__Iterator2 : IDisposable, IEnumerator, IEnumerator<object>
        {
            internal object $current;
            internal int $PC;
            internal RectTransform <$>target;
            internal Vector3 <maxPosition>__3;
            internal Vector3 <minPosition>__2;
            internal RectTransform <parent>__0;
            internal Vector3 <pos>__1;
            internal RectTransform target;

            [DebuggerHidden]
            public void Dispose()
            {
                this.$PC = -1;
            }

            public bool MoveNext()
            {
                uint num = (uint) this.$PC;
                this.$PC = -1;
                switch (num)
                {
                    case 0:
                        this.$current = new WaitForEndOfFrame();
                        this.$PC = 1;
                        return true;

                    case 1:
                        this.<parent>__0 = this.target.parent as RectTransform;
                        this.<pos>__1 = this.target.localPosition;
                        this.<minPosition>__2 = (Vector3) (this.<parent>__0.rect.min - this.target.rect.min);
                        this.<maxPosition>__3 = (Vector3) (this.<parent>__0.rect.max - this.target.rect.max);
                        this.<pos>__1.x = Mathf.Clamp(this.target.localPosition.x, this.<minPosition>__2.x, this.<maxPosition>__3.x);
                        this.<pos>__1.y = Mathf.Clamp(this.target.localPosition.y, this.<minPosition>__2.y, this.<maxPosition>__3.y);
                        this.target.localPosition = this.<pos>__1;
                        this.$PC = -1;
                        break;
                }
                return false;
            }

            [DebuggerHidden]
            public void Reset()
            {
                throw new NotSupportedException();
            }

            object IEnumerator<object>.Current
            {
                [DebuggerHidden]
                get
                {
                    return this.$current;
                }
            }

            object IEnumerator.Current
            {
                [DebuggerHidden]
                get
                {
                    return this.$current;
                }
            }
        }

        [CompilerGenerated]
        private sealed class <Show>c__AnonStorey37
        {
            internal UIGenericMenu.Element element;

            internal void <>m__16()
            {
                this.element.onClick();
                Singleton<UIGenericMenu>.Instance.gameObject.SetActive(false);
            }
        }

        public class Element
        {
            public bool isOn;
            public string name;
            public Action onClick;
        }
    }
}

